﻿using Biz.EF.Gateway.Monitor;
using Biz.EF.Platform.SG;
using Hjt212Parser;
using log4net;
using System.Linq;

namespace Biz.Process
{
    public class CommonProcessor
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CommonProcessor));
        public void Process(string cn)
        {
            if (string.IsNullOrEmpty(cn))
            {
                return;
            }

            using (var gCtx = new GatewayMonitorContext())
            using (var pCtx = new PlatformXpfContext())
            {
                var flag = ProcessFlag.NotProcessed.ToString();
                var items =
                    gCtx.received_msg.Where(t => t.is_length_right)
                        .Where(t => t.processed_flag.Equals(flag))
                        .Where(t => t.cn.Equals(cn))
                        .OrderBy(t => t.datatime)
                        .Take(100)
                        .ToList();
                foreach (var item in items)
                {
                    Log.Info("Processing " + cn + ":" + item.received_string);
                    switch (item.cn)
                    {
                        case "2011":
                            ProcessorData.Process2011(item.received_string);
                            break;
                        case "2051":
                            ProcessorData.ProcessMinuteData(item.received_string);
                            break;
                        case "0000":
                            ProcessorStatus.Process0000(pCtx, item.received_string);
                            break;
                        //case "9999":
                        //ProcessorStatus.Process9999(pCtx, item.received_string);
                        //   break;
                        case "9011":
                            ProcessorResponse.Process9011(pCtx, item.received_string);
                            break;
                        case "9012":
                            ProcessorResponse.Process9012(pCtx, item.received_string);
                            break;
                        case "3071":
                            ProcessorStatus.Process3071(pCtx, item.received_string);
                            break;
                        case "3081":
                            ProcessorStatus.Process3081(pCtx, item.received_string);
                            break;
                        case "3082":
                            ProcessorStatus.Process3082(pCtx, item.received_string);
                            break;
                    }
                    if (!item.cn.Equals("0000") && !item.cn.Equals("3081"))
                    {
                        ProcessorStatus.Process9999(pCtx, item.received_string);
                        ProcessorStatus.Process3083(pCtx, item.received_string);
                    }
                    item.processed_flag = ProcessFlag.ProcessingFinished.ToString();
                }
                gCtx.SaveChanges();
            }
        }
    }
}
